/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package prototype.adaptor;

import java.util.ArrayList;
import java.util.List;
import prototype.persistence.pojo.Goal;
import prototype.persistence.pojo.Scenario;
import prototype.persistence.pojo.Unit;
import prototype.persistence.pojo.User;
import prototype.persistence.pojo.UserHasUnit;
import prototype.runtime.federate.RuntimeUser;
import prototype.runtime.interfaces.RuntimeGoal;
import prototype.runtime.interfaces.RuntimeUnit;

/**
 *
 * @author Server
 */
public class RuntimeUserFactory {

    private static RuntimeUserFactory instance;

    static {
        instance = new RuntimeUserFactory();
    }

    public static RuntimeUserFactory getInstance() {
        return instance;
    }

    public RuntimeUser newRuntimeUser(User user, Scenario scenario) {
        initializeScenario(scenario);
        RuntimeUser runtimeUser = new RuntimeUser(user, scenario);
        List<RuntimeUnit> units = new ArrayList<RuntimeUnit>();
        List<RuntimeGoal> goals = new ArrayList<RuntimeGoal>();

        for (UserHasUnit uhu : user.getUserHasUnits()) {
            units.add(RuntimeUnitFactory.getInstance().newRuntimeUnit(uhu));
        }

        for (Goal goal : user.getGoals()) {
            goals.add(RuntimeGoalFactory.getInstance().newRuntimeGoal(goal));
        }

        runtimeUser.setRuntimeUnits(units);
        runtimeUser.setRuntimeGoals(goals);

        return runtimeUser;
    }

    private void initializeScenario(Scenario scenario) {
        for (User u : scenario.getUsers()) {
            for (UserHasUnit uhu : u.getUserHasUnits()) {
                uhu.setUnit(clone(uhu.getUnit()));
            }
        }
    }

    public static Unit clone(Unit orig) {
        Unit unit = new Unit();
        unit.setAccuracy(orig.getAccuracy().intValue());
        unit.setArmor(orig.getArmor().intValue());
        unit.setClassName(orig.getClassName());
        unit.setFireEffectRadius(orig.getFireEffectRadius().intValue());
        unit.setFirePower(orig.getFirePower().intValue());
        unit.setHealth(orig.getHealth().intValue());
        unit.setId(orig.getId().intValue());
        unit.setJessFile(orig.getJessFile());
        unit.setModelFile(orig.getModelFile());
        unit.setMovementRate(orig.getMovementRate().intValue());
        unit.setName(orig.getName());
        unit.setRangeDistance(orig.getRangeDistance().intValue());
        unit.setReloadTime(orig.getReloadTime().intValue());
        unit.setUserHasUnits(orig.getUserHasUnits());
        return unit;
    }
}
